<?php
namespace app\admin\controller;


use app\common\model\Group;
use think\Db;
use Upload;

class Main extends Base
{

     public function __construct()
    {
        parent::__construct();
        $this->isLogin();
    }


    /**
     * @route('main/index')
     */
    public function index()
    {
        // 获取权限
        $auth = $this->_licenseid['license_data'];
        $menu = model('License')
            ->with(
                ['subs'=>function($query)use($auth){$query

                    ->where('status',1)
                    ->where('sysnav_id', '<>', 0)
                    ->where('license_id','in',$auth)
                    ->order('sort','asc');
                }])
            ->where('license_id','in', $auth)
            ->where('sysnav_id',0)
            ->where('status',1)
            ->order('sort','asc')
            ->select();

        $tvar = array(
            "title" => "首页",
            "time"  => time(),
            "menu"  => $menu,
        );
        $this->assign("data", $tvar);
        return $this->fetch('main/index');
    }

    /**
     * @route('main/welcome')
     */
    public function welcome()
    {
        $tvar = array(
            "title" => "首页",
            "time"  => time(),
        );
        $this->assign("data", $tvar);
        return $this->fetch('main/welcome1');
    }


    /**
     * @route('main/authlist')
     * 权限管理
     */
    public function authlist()
    {
        $groupModel = new Group();

        if(request()->isAjax()){
            if(input('type') == 0){
                $group_id = input('group_id');
                $auth = $groupModel->getOne(array('group_id' => $group_id),'license_data')['license_data'];
                $auths = explode(',',$auth);
                $this->success('','',$auths);
            }else{
                $id = input('id');

                // 获取用户组ID
                $group_id = $id['user'];
                // 删除掉数组第一个
                unset($id['user']);
                // 重新格式化数组成为字符串
                $id = implode(",", $id);
                $rs = $groupModel->edit(array("license_data" => $id,), $group_id);
                if($rs === false){
                    $this->error($groupModel->getError());
                }
                $this->success('操作成功');
            }
        }

        // 获取管理组列表
        $rs = $groupModel->get_list();

        // 获取模块列表
        $auth = $this->_licenseid['license_data'];
        $menu = model('License')
            ->with(
                ['subs'=>function($query)use($auth){$query

                    ->where('status',1)
                    ->where('sysnav_id', '<>', 0)
//                    ->where('license_id','in',$auth)
                    ->order('sort','asc');
                }])
//            ->where('license_id','in', $auth)
            ->where('sysnav_id',0)
            ->where('status',1)
            ->order('sort','asc')
            ->select();


        $tvar = array(
            "title" => "权限管理",
            "time"  => time(),
            "group" => $rs,
        );
        $this->assign('menu', $menu);
        $this->assign('data', $tvar);
        return $this->fetch('main/authlist');
    }


    /**
     * @route('main/quit')
     */
    public function quit()
    {
        cookie('name', null);
        $this->redirect('/login');
    }


    /**
     * 富文本上传图片接口
     * @route('main/ueditor')
     */
    public function ueditor()
    {

        $res = Upload::FileUpload('file','ueditor',1024*1024*10,'jpg,png,jpeg,gif');
        if(!$res['data']){
            $this->result('','2','上传出错');
        }
        $data['src'] = $_SERVER['HTTP_ORIGIN']."/".$res['data']['file_path'];
        $this->result($data,'0','上传成功');
    }



}
